package com.alibaba.aliper.core;

public abstract class LoopThread implements Runnable{
	public LoopThread(){}
	public LoopThread(String name,boolean deamon){
		this.name = name;
		this.deamon = deamon;
	}
	private String name = "default";
	private boolean deamon = false;
	private boolean running = false;
	protected long interval = -1;
	public synchronized void startUp(){
		if(!running){
			this.running = true;
			Thread thread  = new Thread(this,this.name);
			thread.setDaemon(deamon);
			thread.start();
		}
	}
	public synchronized void shutDown(){
		this.running = false;
	}
	@Override
	public final void run() {
		while(this.running){
			if(this.interval > 0){
				sleep(this.interval);
			}
			this.looping();
		}
	}
	
	private static void sleep(long interval) {
		try {
			Thread.sleep(interval);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public abstract void looping();

}
